package Application;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

import Base.*;

public class Main {
    public static void main(String args[]) {
        ArrayList<Computer> array1 = new ArrayList<Computer>();
        ArrayList<Phone> array2 = new ArrayList<Phone>();
        int choice;
        try {
            while (true) {
                System.out.println("输入1：添加computer信息");
                System.out.println("输入2：添加phone信息");
                System.out.println("输入3：设置修改computer信息");
                System.out.println("输入4：设置修改phone信息");
                System.out.println("输入5：查看所有电子设备信息");
                System.out.println("输入6：删除computer信息");
                System.out.println("输入7：删除phone信息");
                System.out.println("输入8：退出系统");

                Scanner in = new Scanner(System.in);
                while(true)
                {
                	try
                	{
                		choice=in.nextInt();
                		break;
                	}
                	catch(Exception e)
                	{
                		System.out.println("输入错误,请重新输入");
                		String bin = in.next();
                	}
                }
                switch (choice) {
                    case 1:
                        computerAdd(array1);//输入1：添加computer信息
                        break;
                    case 2:
                        phoneAdd(array2);//输入2：添加phone信息
                        break;
                    case 3:
                        computerSet(array1);//输入3：设置修改computer信息
                        break;
                    case 4:
                        phoneSet(array2);//输入4：设置修改phone信息
                        break;
                    case 5:
                        witnessAll(array1, array2);//输入5：查看所有电子设备信息
                        break;
                    case 6:
                        computerDelete(array1);
                        break;
                    case 7:
                        phoneDelete(array2);
                        break;
                    case 8:
                        System.out.println("谢谢使用！");//输入6：退出系统
                        System.exit(0);//退出java虚拟机，结束程序
                        in.close();
                }
            }
        }catch(Exception e1){
System.out.println("程序异常，可能是你输入错误，请重试");
        }
        }


    public static void computerAdd(ArrayList<Computer> array1) {
        Scanner in = new Scanner(System.in);

        System.out.println("请输入computer的重量：");
        int weight = in.nextInt();
        System.out.println("请输入computer的价格：");
        int price = in.nextInt();
        System.out.println("请输入computer的耗电量：");
        int powerout = in.nextInt();
        System.out.println("请输入computer的制造商：");
        String maker = in.next();
        System.out.println("请输入computer的内存：");
        int memory = in.nextInt();
        System.out.println("请输入computer的CPU型号：");
        String cputype = in.next();

        Computer c = new Computer();
        c.setCputype(cputype);
        c.setMaker(maker);
        c.setMemory(memory);
        c.setPowerout(powerout);
        c.setPrice(price);
        c.setWeirht(weight);

        array1.add(c);

        System.out.println("添加computer信息成功");
    }

    public static void phoneAdd(ArrayList<Phone> array2) {
        Scanner in = new Scanner(System.in);

        System.out.println("请输入phone的重量：");
        int weight = in.nextInt();
        System.out.println("请输入phone的价格：");
        int price = in.nextInt();
        System.out.println("请输入phone的耗电量：");
        int powerout = in.nextInt();
        System.out.println("请输入phone的制造商：");
        String maker = in.next();
        System.out.println("请输入phone的系统：");
        String system = in.next();
        System.out.println("请输入phone的存储空间：");
        String memoryspace = in.nextLine();

        Phone p = new Phone();
        p.setMaker(maker);
        p.setMemorySpace(memoryspace);
        p.setPowerout(powerout);
        p.setPrice(price);
        p.setSystem(system);
        p.setWeirht(weight);

        array2.add(p);

        System.out.println("添加phone信息成功");
    }

    public static void computerSet(ArrayList<Computer> array1) {
        if (array1.size() == 0) {
            System.out.println("无信息");
        } else {
            Scanner in = new Scanner(System.in);
            System.out.println("输入修改computer的序号：");
            int xuhao = in.nextInt();
            System.out.println("请输入computer的新重量：");
            int weight = in.nextInt();
            System.out.println("请输入computer的新价格：");
            int price = in.nextInt();
            System.out.println("请输入computer的新耗电量：");
            int powerout = in.nextInt();
            System.out.println("请输入computer的新制造商：");
            String maker = in.next();
            System.out.println("请输入computer的新内存：");
            int memory = in.nextInt();
            System.out.println("请输入computer的新CPU型号：");
            String cputype = in.next();

            Computer c = new Computer();
            c.setCputype(cputype);
            c.setMaker(maker);
            c.setMemory(memory);
            c.setPowerout(powerout);
            c.setPrice(price);
            c.setWeirht(weight);

            array1.set(xuhao, c);
        }
    }

    public static void phoneSet(ArrayList<Phone> array2) {
        if (array2.size() == 0) {
            System.out.println("无信息");
        } else {
            Scanner in = new Scanner(System.in);
            System.out.println("输入修改phone的序号：");
            int xuhao2 = in.nextInt();
            System.out.println("请输入phone的新重量：");
            int weight = in.nextInt();
            System.out.println("请输入phone的新价格：");
            int price = in.nextInt();
            System.out.println("请输入phone的新耗电量：");
            int powerout = in.nextInt();
            System.out.println("请输入phone的新制造商：");
            String maker = in.next();
            System.out.println("请输入phone的新系统：");
            String system = in.next();
            System.out.println("请输入phone的新存储空间：");
            String memoryspace = in.nextLine();

            Phone p = new Phone();
            p.setMaker(maker);
            p.setMemorySpace(memoryspace);
            p.setPowerout(powerout);
            p.setPrice(price);
            p.setSystem(system);
            p.setWeirht(weight);

            array2.set(xuhao2, p);
        }
    }

    public static void computerDelete(ArrayList<Computer> array1) {
        Scanner in = new Scanner(System.in);
        System.out.println("输入要删除的computer的序号：");
        int index1 = in.nextInt();
        array1.remove(index1);

        System.out.println("删除成功！");
    }

    public static void phoneDelete(ArrayList<Phone> array2) {
        Scanner in = new Scanner(System.in);
        System.out.println("输入要删除的phone的序号：");
        int index2 = in.nextInt();
        array2.remove(index2);
    }

    public static void witnessAll(ArrayList<Computer> array1, ArrayList<Phone> array2) {
        if (array1.size() == 0 && array2.size() == 0) {
            System.out.println("暂无信息，请添加信息");
        } else {
            for (int i = 0; i < array1.size(); i++) {
                Computer c = array1.get(i);
                System.out.println("computer" + "\t" + "重量" + "\t" + "价格" + "\t" + "耗电量" + "\t" + "制造商" + "\t" + "内存" + "\t" + "CPU型号");
                System.out.println(i + "\t\t" + c.getWeight() + "\t" + c.getPrice() + "\t" + c.getPowerout() + "\t" + c.getMaker() + "\t" + c.getMemory() + "\t" + c.getCputype());
            }
            for (int t = 0; t < array2.size(); t++) {
                Phone p = array2.get(t);
                System.out.println("phone" + "\t" + "重量" + "\t" + "价格" + "\t" + "耗电量" + "\t" + "制造商" + "\t" + "系统" + "\t" + "存储空间");
                System.out.println(t + "\t" + p.getWeight() + "\t" + p.getPrice() + "\t" + p.getPowerout() + "\t" + p.getMaker() + "\t" + p.getSystem() + "\t" + p.getMemorySpace());
            }
        }
    }
}
